package com.tongtech.system.controller;

import com.tongtech.common.core.web.controller.BaseController;
import com.tongtech.common.core.web.domain.AjaxResult;
import com.tongtech.common.log.annotation.Log;
import com.tongtech.common.log.enums.BusinessType;
import com.tongtech.common.security.annotation.RequiresPermissions;
import com.tongtech.common.security.utils.SecurityUtils;
import com.tongtech.system.domain.InformationCategorySubscribe;
import com.tongtech.system.service.IInformationCategorySubscribeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 资讯分类订阅信息
 *
 * @author xingyc
 */
@RestController
@RequestMapping("/infoCategorySubscribe")
public class InformationCategorySubscribeController extends BaseController
{
    @Autowired
    private IInformationCategorySubscribeService infoCategoryService;

    /**
     * 获取资讯分类订阅列表
     */
    @RequiresPermissions("info:category:list")
    @GetMapping("/list")
    public AjaxResult list(InformationCategorySubscribe infoCategory)
    {
        List<InformationCategorySubscribe> infoCategorys = infoCategoryService.selectInformationCategorySubscribeList(infoCategory);
        return success(infoCategorys);
    }

    /**
     * 根据资讯分类订阅编号获取详细信息
     */
    @RequiresPermissions("info:category:query")
    @GetMapping(value = "/{infoCategoryId}")
    public AjaxResult getInfo(@PathVariable Long infoCategoryId)
    {
        return success(infoCategoryService.selectInformationCategorySubscribeById(infoCategoryId));
    }

    /**
     * 新增资讯分类订阅
     */
    @RequiresPermissions("info:category:add")
    @Log(title = "资讯分类订阅管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody InformationCategorySubscribe infoCategory)
    {
        infoCategory.setCreateBy(SecurityUtils.getUsername());
        return toAjax(infoCategoryService.insertInformationCategorySubscribe(infoCategory));
    }

    /**
     * 修改资讯分类订阅
     */
    @RequiresPermissions("info:category:edit")
    @Log(title = "资讯分类订阅管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody InformationCategorySubscribe dept)
    {
        dept.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(infoCategoryService.updateInformationCategorySubscribe(dept));
    }

    /**
     * 删除资讯分类订阅
     */
    @RequiresPermissions("info:category:remove")
    @Log(title = "资讯分类订阅管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deptId}")
    public AjaxResult remove(@PathVariable Long deptId)
    {
        return toAjax(infoCategoryService.deleteInformationCategorySubscribeById(deptId));
    }

    /**
     * 删除资讯分类订阅
     */
    @RequiresPermissions("info:category:remove")
    @Log(title = "资讯分类订阅管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/byCategory/{categoryId}")
    public AjaxResult removeByUserIdCategoryId(@PathVariable Long categoryId)
    {
        Long userId = SecurityUtils.getUserId();
        return toAjax(infoCategoryService.deleteInformationCategorySubscribeByUserIdCategoryId(userId, categoryId));
    }
}
